#ifndef RUN_TIME_INFO_BAR_HPP_
#define RUN_TIME_INFO_BAR_HPP_

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

namespace lge2d
{

class RunTimeInfoBar
{
public:
	RunTimeInfoBar();
	RunTimeInfoBar(sf::RenderWindow* window, sf::Font* font);

	void Initialize();

	bool OnEvent(const sf::Event& event);
	void Update(float dt);
	void Render() const;

	void SetFont(sf::Font* font) {m_font = font;}
	void SetWindow(sf::RenderWindow* window) {m_window = window;}

	virtual ~RunTimeInfoBar();

private:
	class FPSCounter
	{
	public:
		FPSCounter() :
				m_clock(), m_frame_count(0), m_fps(0), m_num_count_time_on_frame(
						24) // 24 good frames count
		{
		}
		void AddOneFrame()
		{
			++m_frame_count;
			if (m_frame_count == m_num_count_time_on_frame)
			{
				m_fps = int(m_frame_count / m_clock.GetElapsedTime());
				m_frame_count = 0;
				m_clock.Reset();
			}
		}
		int GetFPS() const
		{
			return m_fps;
		}
	private:
		sf::Clock m_clock;
		int m_frame_count;
		int m_fps;
		const int m_num_count_time_on_frame;
	};

	class IntRenderer
	{
	public:
		IntRenderer():m_font(0), m_prev_int_value(-1)
		{
		}
		explicit IntRenderer(sf::Font* font) :
				m_font(font), m_prev_int_value(-1)
		{
		}
		void Initialize()
		{
			assert(NULL != m_font);
			m_text.SetFont(*m_font);
			m_text.SetSize(m_font->GetCharacterSize());
		}
		void SetFont(sf::Font* font) {m_font = font;}
		void Render(sf::RenderWindow* window, int value) const
		{
			Update(value);
			window->Draw(m_text);
		}
	private:
		void Update(int value) const
		{
			if (m_prev_int_value != value)
			{
				char buff[32] =
				{ 0 };
				snprintf(buff, sizeof(buff), "%d", value);
				m_text.SetText(buff);
				m_prev_int_value = value;
			}
		}
		sf::Font* m_font;
		mutable int m_prev_int_value;
		mutable sf::String m_text;
	};

	RunTimeInfoBar(const RunTimeInfoBar& other);
	RunTimeInfoBar& operator=(const RunTimeInfoBar& other);

	sf::RenderWindow* m_window;
	sf::Font* m_font;
	sf::Vector2<int> m_bar_pos;

	FPSCounter m_fps_counter;
	IntRenderer m_int_fps_renderer;
};

} /* namespace lge2d */
#endif /* RUN_TIME_INFO_BAR_HPP_ */
